using UnityEngine;

public class cameraControl : MonoBehaviour
{
	public car car1;

	public Transform target;

	public float xSpeed;

	public float ySpeed;

	public float zoomSpeed = 0.1f;

	public float minValueY = -20f;

	public float maxValueY = 80f;

	private Quaternion rotation;

	private Vector3 position;

	private float distance = 5f;

	private float x;

	private float y;

	private float tempX;

	private float tempY;

	public float smoothTime = 0.1f;

	private float xSmooth;

	private float ySmooth;

	private void Start()
	{
		Vector3 eulerAngles = base.transform.eulerAngles;
		x = eulerAngles.y;
		Vector3 eulerAngles2 = base.transform.eulerAngles;
		y = eulerAngles2.x;
	}

	private void Update()
	{
		if (car1.controlled && target != null)
		{
			x += UnityEngine.Input.GetAxis("Mouse X") * xSpeed;
			y -= UnityEngine.Input.GetAxis("Mouse Y") * ySpeed;
			tempX = Mathf.SmoothDamp(tempX, x, ref xSmooth, smoothTime);
			tempY = Mathf.SmoothDamp(tempY, y, ref ySmooth, smoothTime);
			distance += UnityEngine.Input.GetAxis("Mouse ScrollWheel") * (0f - zoomSpeed);
			y = ClampAngle(y, minValueY, maxValueY);
			rotation = Quaternion.Euler(tempY, tempX, 0f);
			position = rotation * new Vector3(0f, 0f, 0f - distance) + target.position;
			Camera.main.transform.rotation = rotation;
			Camera.main.transform.position = position;
		}
	}

	private float ClampAngle(float angle, float min, float max)
	{
		if (angle < -360f)
		{
			angle += 360f;
		}
		if (angle > 360f)
		{
			angle -= 360f;
		}
		return Mathf.Clamp(angle, min, max);
	}
}
